//
// Created by shenbin on 2022/4/17.
// https://leetcode-cn.com/problems/min-stack/
//

#ifndef ALGORITHM_TRAINNING_C02_LC155_H
#define ALGORITHM_TRAINNING_C02_LC155_H

#include <stack>

using namespace std;

class LeetCode155 {
public:
    /**
     * 核心：多设计一个存储最小值的栈，每次操作时比较并存储最小值；
     */
    class MinStack {
    public:
        void push(int val) {
            s.push(val);
            if (preMin.empty()) {
                preMin.push(val);
            } else {
                preMin.push(min(preMin.top(), val));
            }

        }

        void pop() {
            s.pop();
            preMin.pop();

        }

        int top() {
            return s.top();

        }

        int getMin() {
            return preMin.top();

        }

    private:
        stack<int> s;
        stack<int> preMin;
    };

};

#endif //ALGORITHM_TRAINNING_C02_LC155_H
